<?php

class Notification_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function create_notification($target_fb_id, $message) {
		$sql = "INSERT INTO notification (target_fb_id, message, date_created) VALUES (?, ?, NOW())";		
		$result = $this->db->query($sql, array($target_fb_id, $message));
		
		return $result;
	}

	function get_notifications($fb_id) {
		$sql = "SELECT * FROM notification WHERE target_fb_id = ? ORDER BY date_created DESC LIMIT 0, 300";
		$query = $this->db->query($sql, $fb_id);
			
		$data = array();
		if($query->num_rows() > 0){
			foreach($query->result_array() AS $row){
				$data[] = $row;
			}
		}
		return $data;
	}
	
	function get_new_notifications($fb_id){
		$sql = "SELECT * FROM notification WHERE target_fb_id = ? AND is_new = 1 ORDER BY date_created DESC";
		$query = $this->db->query($sql, $fb_id);
			
		$data = array();
		if($query->num_rows() > 0){
			foreach($query->result_array() AS $row){
				$data[] = $row;
			}
		}
		return $data;
	}
	
	function clear_new_notifications($fb_id){
		$sql = "UPDATE notification SET is_new = 0 WHERE target_fb_id = ?";
		$query = $this->db->query($sql, $fb_id);
	}
}

?>